package com.wnxy.product.controller;

import com.wnxy.sddfp.common.result.Result;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("msg")
public class MessageController {
    /**
     * 消息发送模板
     */
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public ResponseEntity<String> sendEmail(String address) {
        //消息
        String context = "中奖了500w, 5分钟之后领取！";

        // 发送消息到RabbitMQ队列中 参数1：队列名称 参数2：消息内容
        rabbitTemplate.convertAndSend("msg.queue", context);

        return ResponseEntity.ok("消息发送成功");
    }

    @GetMapping("/sendObj")
    public ResponseEntity<String> sendObj() {

        Result result = new Result();
        result.setCode(10086);
        result.setMessage("枪毙10分钟");

        // 发送消息到RabbitMQ队列中 参数1：队列名称 参数2：消息内容
        rabbitTemplate.convertAndSend("obj.queue", result);

        return ResponseEntity.ok("消息发送成功");
    }

    @GetMapping("/simple")
    public ResponseEntity<String> simpleQueue() {

        String msg = "简单队列";

        // 发送消息到RabbitMQ队列中 参数1：队列名称 参数2：消息内容
        rabbitTemplate.convertAndSend("simple.queue", msg);

        return ResponseEntity.ok("消息发送成功");
    }

    @GetMapping("/work")
    public ResponseEntity<String> workQueue() {

        String msg = "";

        for (int i = 0; i < 10; i++) {

            int num = i;
            msg = "枪毙" + num + "分钟";
            // 发送消息到RabbitMQ队列中 参数1：队列名称 参数2：消息内容
            rabbitTemplate.convertAndSend("work.queue", msg);
        }

        return ResponseEntity.ok("消息发送成功");
    }

    @GetMapping("/fanoutExchange1")
    public ResponseEntity<String> fanoutExchange1() {
        //消息
        String context = "中奖了500w, 5分钟之后领取！";

        // 发送消息到RabbitMQ队列中 参数1：队列名称 参数2：消息内容
        rabbitTemplate.convertAndSend("fanout.exchange", null, context);

        return ResponseEntity.ok("消息发送成功");
    }

    @GetMapping("/fanoutExchange2")
    public ResponseEntity<String> fanoutExchange2() {
        //消息
        String context = "亏了500w, 5分钟之后跳楼！";

        // 发送消息到RabbitMQ队列中 参数1：队列名称 参数2：消息内容
        rabbitTemplate.convertAndSend("fanout.exchange", null, context);

        return ResponseEntity.ok("消息发送成功");
    }

    @GetMapping("/directExchange")
    public ResponseEntity<String> directExchange() {
        //消息
        String context = "我是帅哥";
        String context1 = "handsomeBoy";

        // 发送消息到RabbitMQ队列中 参数1：队列名称 参数2：消息内容
        rabbitTemplate.convertAndSend("direct.exchange", "shuai", context);
        rabbitTemplate.convertAndSend("direct.exchange", "handsome", context1);

        return ResponseEntity.ok("消息发送成功");
    }

    @GetMapping("/topicExchange")
    public ResponseEntity<String> topicExchange() {
        //消息
        String context = "我是帅哥";
        String context0 = "我是美女";
        String context1 = "handsomeBoy";
        String context2 = "beautifulGirl";

        // 发送消息到RabbitMQ队列中 参数1：队列名称 参数2：消息内容
        rabbitTemplate.convertAndSend("topic.exchange", "shuaige.kiss.news", context);
        rabbitTemplate.convertAndSend("topic.exchange", "shuaige.asss.news", context0);
        rabbitTemplate.convertAndSend("topic.exchange", "girl.news", context1);
        rabbitTemplate.convertAndSend("topic.exchange", "handsome.news", context2);

        return ResponseEntity.ok("消息发送成功");
    }

//    @GetMapping("/send/confirm/{msg}")
//    public String sendMsg(@PathVariable("msg") String msg){
//
//        /**
//         * 参数1：交换机
//         * 参数2：路由
//         * 参数3：msg
//         */
//        rabbitTemplate.convertAndSend(
//                "confirm.direct.exchange",
//                "msg.confirm",
//                msg);
//        return "发送成功...";
//    }

    @GetMapping("/send/dlx/{msg}")
    public String sendDLXMsg(@PathVariable("msg") String msg){

        /**
         * 参数1：交换机
         * 参数2：路由
         * 参数3：msg
         */
        rabbitTemplate.convertAndSend(
                "normal.exchange",
                "city.gz",
                msg);
        return "发送成功...";
    }

}